package com.bingkaimedia.merchantbeta.libs;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class JSONfunctions {

	
	
	
	public static String getStringContent(String uri)  {

		StringBuilder sb = new StringBuilder();
		
	    try {
	    	//System.out.println("masuk getStringContent"); 
	    	
	    	HttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 30000;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            int timeoutSocket = 30000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
            HttpClient httpclient = new DefaultHttpClient(httpParameters);
	    	
	    	//HttpClient client = new DefaultHttpClient();
	        HttpGet request = new HttpGet();
	        request.setURI(new URI(uri));
	        HttpResponse response = httpclient.execute(request);
	        
	        InputStream ips  = response.getEntity().getContent();
	        BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));

	       
	        String s;
	        while(true )
	        {
	            s = buf.readLine();
	            if(s==null || s.length()==0)
	                break; 
	            sb.append(s);

	        }
	        buf.close();
	        ips.close();  
	        return sb.toString();

	    } catch (ConnectTimeoutException e) {
	        //Here Connection TimeOut excepion    
	    	//Toast.makeText(, "Your connection timedout", 10000).show();

			System.out.println("catch 1");
			System.out.println(e);
			System.out.println("habis waktu");
	    }catch(Exception e){

			System.out.println("catch 2");
			System.out.println(e);
	    	System.out.println("masuk catch exception");
	        //Log.e("log_tag", "Error in http connection "+e.toString());
	    } 
	    finally {
	    	// any cleanup code...
	    }
	    
	    
	    return sb.toString();
	} 
	
	
	
	public static String getStringContent2(String uri)  {

		StringBuilder sb = new StringBuilder();
		ArrayList nameValuePairs = new ArrayList();
		InputStream is = null;
		String result = null;
		
	    try {
	    	//System.out.println("masuk getStringContent"); 
	    	
	    	HttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 30000;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            int timeoutSocket = 30000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
            HttpClient httpclient = new DefaultHttpClient(httpParameters);
	    	
            HttpPost httppost = new HttpPost(uri);
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
	       
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
			sb = new StringBuilder();
			sb.append(reader.readLine() + "\n");
			String line="0";
	     
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			
			is.close();
			result=sb.toString();
			//System.out.println("result");
			//System.out.println(result);
	         
	        return result;

	    } catch (ConnectTimeoutException e) {
	        //Here Connection TimeOut excepion    
	    	//Toast.makeText(, "Your connection timedout", 10000).show();

			System.out.println("catch 1");
			System.out.println(e);
			System.out.println("habis waktu");
	    }catch(Exception e){

			System.out.println("catch 2");
			System.out.println(e);
	    	System.out.println("masuk catch exception");
	    	//Log.e("log_tag", "Error in http connection "+e.toString());
	    } 
	    finally {
	    	// any cleanup code...
	    }
	    
	    
	    return sb.toString();
	} 
}
